<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Click the Ball Game</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400&family=Merriweather:wght@700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Roboto', sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f9f9f9;
            color: #333;
        }
        #title {
            font-size: 36px;
            font-weight: bold;
            margin-top: 20px;
            color: #5b8db8;
            text-align: center;
            font-family: 'Merriweather', serif;
        }
        #gameContainer {
            position: relative;
            width: 90%;
            max-width: 1000px;
            height: 600px;
            background-color: #ffffff;
            border: 2px solid #333;
            margin-top: 20px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            border-radius: 10px;
        }
        .ball {
            position: absolute;
            border-radius: 50%;
            cursor: pointer;
            transition: transform 0.2s ease;
            cursor: default;
        }
        #controls {
            margin-top: 20px;
            display: flex;
            justify-content: space-around;
            align-items: center;
            width: 90%;
            max-width: 1000px;
            flex-wrap: wrap;
        }
        #startButton, #stopButton {
            padding: 10px 20px;
            font-size: 18px;
            cursor: pointer;
            background-color: #5b8db8;
            color: #fff;
            border: none;
            border-radius: 5px;
            transition: background-color 0.3s ease;
            margin: 10px;
        }
        #startButton:hover, #stopButton:hover {
            background-color: #3c6e91;
        }
        #sizeInput, #colorInput, #countInput {
            margin-left: 10px;
            padding: 5px;
            border: 1px solid #ced4da;
            border-radius: 5px;
            font-size: 16px;
            margin-top: 10px;
        }
        #stats {
            display: flex;
            justify-content: space-around;
            margin-top: 20px;
            width: 90%;
            max-width: 1000px;
            font-size: 20px;
            background-color: #eef3f6;
            padding: 10px 0;
            border-radius: 10px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            flex-wrap: wrap;
        }
        #stats div {
            text-align: center;
            flex: 1;
            margin: 5px 0;
        }
        #highScore {
            margin-top: 20px;
            font-size: 24px;
            color: #5b8db8;
            font-weight: bold;
        }
        #footer {
            margin-top: 40px;
            font-size: 16px;
            color: #555;
            text-align: center;
            width: 90%;
        }
        #footer a {
            color: #5b8db8;
            text-decoration: none;
        }
        #footer a:hover {
            color: #3c6e91;
            text-decoration: underline;
        }
        @media (max-width: 768px) {
            #title {
                font-size: 28px;
            }
            #gameContainer {
                height: 600px;
            }
            #controls {
                flex-direction: column;
                align-items: center;
            }
            #startButton, #stopButton {
                width: 100%;
            }
            #sizeInput, #colorInput, #countInput {
                margin-left: 0;
                margin-top: 10px;
                width: 100%;
                text-align: center;
            }
            #stats {
                flex-direction: column;
            }
            #highScore {
                font-size: 20px;
            }
        }
    </style>
</head>
<body>
    <div id="title">🎯 Click the Ball Game</div>
    <div id="controls">
       <div><label for="sizeInput">⚙️ Ball Size (10-100px):</label><input type="number" id="sizeInput" min="10" max="100" value="30"></div>
       <div><label for="countInput">🎯 Ball Count (1-10):</label><input type="number" id="countInput" min="1" max="10" value="1"></div>
       <div><label for="colorInput">🎨 Ball Color:</label><input type="color" id="colorInput" value="#ff6347"></div>
        <button id="startButton">🚀 Start Game</button>
        <button id="stopButton" style="display: none;">🛑 Stop Game</button>
    </div>
    <div id="stats">
        <div>⏳ Time Left: <span id="timer">30</span> seconds</div>
        <div>🏆 Score: <span id="score">0</span></div>
        <div>🎯 Accuracy: <span id="accuracy">0</span>%</div>
    </div>
    <div id="gameContainer"></div>
    <div id="highScore">🥇 High Score: <span id="highScoreValue">0</span></div>
    <div id="footer">
        Game repository: <a href="https://github.com/liaoyanqing666/liaoyanqing666.github.io" target="_blank">https://github.com/liaoyanqing666/liaoyanqing666.github.io</a>

        Game author: <a href="https://siyuanli.tech" target="_blank">Siyuan Li</a>
    </div>
    <script>
        const gameContainer = document.getElementById("gameContainer");
        const scoreDisplay = document.getElementById("score");
        const timerDisplay = document.getElementById("timer");
        const accuracyDisplay = document.getElementById("accuracy");
        const highScoreDisplay = document.getElementById("highScoreValue");
        const startButton = document.getElementById("startButton");
        const stopButton = document.getElementById("stopButton");
        const sizeInput = document.getElementById("sizeInput");
        const colorInput = document.getElementById("colorInput");
        const countInput = document.getElementById("countInput");
        let score = 0;
        let totalClicks = 0;
        let timeLeft = 30;
        let isGameStarted = false;
        let ballSize = 30;
        let ballColor = "#ff6347";
        let highScore = 0;

        function checkCollision(x, y, size) {
            const balls = gameContainer.getElementsByClassName("ball");
            for (const ball of balls) {
                const ballX = parseInt(ball.style.left);
                const ballY = parseInt(ball.style.top);
                const dx = x - ballX;
                const dy = y - ballY;
                if (dx * dx + dy * dy < size * size) return true;
            }
            return false;
        }

        function createBall() {
            const ball = document.createElement("div");
            ball.classList.add("ball");
            const maxX = gameContainer.clientWidth - ballSize;
            const maxY = gameContainer.clientHeight - ballSize;
            
            let randomX, randomY;
            do {
                randomX = Math.floor(Math.random() * maxX);
                randomY = Math.floor(Math.random() * maxY);
            } while (checkCollision(randomX, randomY, ballSize));
            
            ball.style.width = ballSize + "px";
            ball.style.height = ballSize + "px";
            ball.style.backgroundColor = ballColor;
            ball.style.left = randomX + "px";
            ball.style.top = randomY + "px";
            ball.addEventListener("click", () => {
                ball.remove();
                score++;
                totalClicks++;
                updateScore();
                updateAccuracy();
                createBall();
            });
            gameContainer.appendChild(ball);
        }

        function updateScore() { scoreDisplay.textContent = score; }
        function updateAccuracy() { 
            accuracyDisplay.textContent = Math.floor((totalClicks ? score / totalClicks : 0) * 100); 
        }

        startButton.addEventListener("click", () => {
            if (!isGameStarted) {
                isGameStarted = true;
                score = 0;
                totalClicks = 0;
                timeLeft = 30;
                ballSize = parseInt(sizeInput.value);
                ballColor = colorInput.value;
                ballCount = parseInt(countInput.value);
                
                gameContainer.innerHTML = "";
                for (let i = 0; i < ballCount; i++) createBall();
                
                timerInterval = setInterval(updateTimer, 1000);
                startButton.style.display = "none";
                stopButton.style.display = "block";
            }
        });

        stopButton.addEventListener("click", () => {
            if (isGameStarted) {
                endGame();
            }
        });

        gameContainer.addEventListener("click", (event) => {
            if (isGameStarted && !event.target.classList.contains("ball")) {
                totalClicks++;
                updateAccuracy();
            }
        });

        function updateTimer() {
            timeLeft--;
            timerDisplay.textContent = timeLeft;
            if (timeLeft <= 0) {
                clearInterval(timerInterval);
                endGame();
            }
        }

        function endGame() {
            clearInterval(timerInterval);
            isGameStarted = false;
            startButton.style.display = "block";
            stopButton.style.display = "none";
            gameContainer.innerHTML = "";
            if (score > highScore) {
                highScore = score;
                highScoreDisplay.textContent = highScore;
                alert(`🎉 New High Score! Score: ${score}`);
            } else {
                alert(`🎉 Game Over! Score: ${score}`);
            }
        }
    </script>
</body>
</html>